﻿using System;
using System.IO;
using XNA_Game_Engine.Canvas;
using XNA_Game_Engine.Sounds;

namespace XNA_Game_Engine.FileManagment
{
    class SaveGameManager
    {
        /****
         * Chamada ocorre na criacao do Core_Engine.
         * Ler na mesma ordem na qual os dados foram salvos.
        ****/
        public static void Load()
        {
            try
            {
                using (StreamReader buffer = new StreamReader("save.dat") )
                {
                    String line = buffer.ReadLine();
                    ActionsHandler.unlockedLevels = int.Parse(line);

                    line = buffer.ReadLine();
                    bool mute = bool.Parse(line);
                    if (mute)
                        SoundController.mute();

                    line = buffer.ReadLine();
                    bool fullScreen = bool.Parse(line);
                    if (fullScreen)
                        ActionsHandler.Game.graphics.ToggleFullScreen();

                    line = buffer.ReadLine();
                    bool enableTips = bool.Parse(line);
                    if (!enableTips)
                        ActionsHandler.showTips = false;


                    Console.WriteLine("File Loaded!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Savegame not found!");
            }
        }


        /****
        /* Invoca cada vez que queira salvar os dados.
         * Adicionar dados a mais no arquivo se precisar, lembrar de quebrar a linha entre cada dado.
        ****/
        public static void Save()
        {
            try
            {
                using (StreamWriter buffer = new StreamWriter("save.dat"))
                {
                    buffer.Write( ActionsHandler.unlockedLevels );
                    buffer.Write("\n");
                    buffer.Write( SoundController.isSoundMuted() );
                    buffer.Write("\n");
                    buffer.Write( ActionsHandler.Game.graphics.IsFullScreen );
                    buffer.Write("\n");
                    buffer.Write(ActionsHandler.showTips);

                    Console.WriteLine("File Saved!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error while saving the game");
            }
        }
    }
}
